package com.myapp.business;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Named
@Stateless
public class EmployeeGetter {
	@PersistenceContext(unitName="myDb")
	private EntityManager entityManager;
	
	public static class EmployeeInfo {
		private Integer employeeId;
		private String firstName;
		private String lastName;
		
		public EmployeeInfo(Integer employeeId, String firstName, String lastName) {
			this.employeeId = employeeId;
			this.firstName = firstName;
			this.lastName = lastName;
		}
		
		public Integer getEmployeeId() {
			return employeeId;
		}
		
		public String getFirstName() {
			return firstName;
		}
		
		public String getLastName() {
			return lastName;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<EmployeeInfo> getEmployeeList() {
		Query query = entityManager.createNativeQuery("SELECT employee_id, first_name, last_name FROM employee");
		List<Object[]> resultList = query.getResultList();
		List<EmployeeInfo> employeeList = new ArrayList<EmployeeInfo>();
		for(Object []elem : resultList) {
			Integer employeeId = (Integer)elem[0];
			String firstName = (String)elem[1];
			String lastName = (String)elem[2];
			EmployeeInfo employeeInfo = new EmployeeInfo(employeeId, firstName, lastName);
			employeeList.add(employeeInfo);
		}
		return employeeList;
	}
}
